# CMP0167 策略行为演示
cmake_minimum_required(VERSION 3.20)

# 演示不同策略设置的行为差异

message("=== CMP0167 策略演示 ===")

# 检查策略是否可用
if(POLICY CMP0167)
    message(STATUS "CMP0167 策略可用")

    # 演示 OLD 行为
    message("\n--- OLD 策略行为 ---")
    cmake_policy(PUSH)  # 保存当前策略状态
    cmake_policy(SET CMP0167 OLD)

    find_package(Boost QUIET)
    if(Boost_FOUND)
        message(STATUS "OLD: Boost ${Boost_VERSION} found via FindBoost module")
        message(STATUS "OLD: Available variables:")
        message(STATUS "  - Boost_LIBRARIES: ${Boost_LIBRARIES}")
        message(STATUS "  - Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
        message(STATUS "  - Boost_DEFINITIONS: ${Boost_DEFINITIONS}")

        # 检查可用的目标
        if(TARGET Boost::headers)
            message(STATUS "  - Boost::headers target: Available")
        else()
            message(STATUS "  - Boost::headers target: Not available")
        endif()
    else()
        message(STATUS "OLD: Boost not found via FindBoost module")
    endif()

    cmake_policy(POP)  # 恢复策略状态

    # 演示 NEW 行为
    message("\n--- NEW 策略行为 ---")
    cmake_policy(PUSH)
    cmake_policy(SET CMP0167 NEW)

    find_package(Boost QUIET)
    if(Boost_FOUND)
        message(STATUS "NEW: Boost ${Boost_VERSION} found via BoostConfig.cmake")
        message(STATUS "NEW: Using upstream Boost configuration")

        # 检查可用的目标
        if(TARGET Boost::headers)
            message(STATUS "  - Boost::headers target: Available")
        else()
            message(STATUS "  - Boost::headers target: Not available")
        endif()

        # 新策略下，传统变量可能不可用
        if(DEFINED Boost_LIBRARIES)
            message(STATUS "  - Boost_LIBRARIES: ${Boost_LIBRARIES}")
        else()
            message(STATUS "  - Boost_LIBRARIES: Not defined (expected in NEW policy)")
        endif()
    else()
        message(STATUS "NEW: Boost not found via BoostConfig.cmake")
        message(STATUS "  This could mean:")
        message(STATUS "  - Boost < 1.70 (no CMake config support)")
        message(STATUS "  - Boost not properly installed with CMake config")
    endif()

    cmake_policy(POP)

else()
    message(STATUS "CMP0167 策略不可用 (CMake < 3.30)")
    message(STATUS "当前使用传统的 FindBoost 模块")

    find_package(Boost QUIET)
    if(Boost_FOUND)
        message(STATUS "Boost ${Boost_VERSION} found via FindBoost module")
    endif()
endif()

message("\n=== 演示结束 ===")

# 实际项目中的推荐做法
message("\n=== 推荐的兼容性处理 ===")
if(POLICY CMP0167)
    cmake_policy(SET CMP0167 NEW)
    message(STATUS "设置为 NEW 策略 - 使用现代 Boost 配置")
else()
    message(STATUS "使用传统 FindBoost 模块")
endif()

find_package(Boost REQUIRED QUIET)
message(STATUS "最终选择：Boost ${Boost_VERSION}")

# 根据可用目标选择链接方式
if(TARGET Boost::headers)
    message(STATUS "推荐链接方式：target_link_libraries(app Boost::headers)")
elseif(DEFINED Boost_LIBRARIES)
    message(STATUS "备选链接方式：target_link_libraries(app \${Boost_LIBRARIES})")
else()
    message(WARNING "无法确定 Boost 链接方式")
endif()
